#pragma execution_character_set("utf-8")
#include "LoginFormandRegister.h"
#include <QApplication>// 引入QApplication头文件
#include <QMainWindow>// 引入QMainWindow头文件
#include <QLabel>// 引入QLabel头文件
#include <QLineEdit>// 引入QLineEdit头文件
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QStackedLayout>
#include <QPalette>
#include <QPixmap>
#include <QMessageBox>
#include <QDebug>
#include <QFont>
#include <QGraphicsDropShadowEffect>
LoginFormandRegister::LoginFormandRegister(QWidget* parent)
        : QWidget(parent)
{
    setFixedSize(1000, 800);  // 设置窗口固定大小
    // 设置窗口标题
    setWindowTitle("登录界面");

    // 设置背景图片并缩放以填充窗口
    QPixmap bkgnd(":/imagesbiji/bgimages.png");
    bkgnd = bkgnd.scaled(size(), Qt::IgnoreAspectRatio);
    QPalette palette;
    palette.setBrush(QPalette::Window, bkgnd);
    this->setPalette(palette);

    // 创建中央登录框（居中白色半透明盒子）
    QWidget* centralWidget = new QWidget(this);
    centralWidget->setFixedSize(420, 360); // 加大一点
    centralWidget->move((width() - centralWidget->width()) / 2, (height() - centralWidget->height()) / 2);
    centralWidget->setStyleSheet(
            "background-color: rgba(255, 255, 255, 220);"
            "border-radius: 15px;"
            "border: 1px solid rgba(200, 200, 200, 100);"
    );

    // 添加阴影效果（可选）
    QGraphicsDropShadowEffect* shadow = new QGraphicsDropShadowEffect();
    shadow->setBlurRadius(25);
    shadow->setOffset(0, 5);
    shadow->setColor(QColor(0, 0, 0, 80));
    centralWidget->setGraphicsEffect(shadow);

    // 主垂直布局
    QVBoxLayout* mainLayout = new QVBoxLayout(centralWidget);
    mainLayout->setSpacing(15);  // 元素之间距离
    mainLayout->setContentsMargins(20, 20, 20, 20);  // 内边距

    // 顶部切换按钮：账户登录、邮箱登录、注册
    QHBoxLayout* tabLayout = new QHBoxLayout();
    QPushButton* accountLoginBtn = new QPushButton("账户登录");
    QPushButton* emailLoginBtn = new QPushButton("邮箱登录");
    QPushButton* registerBtn = new QPushButton("用户注册");

    // 设置按钮样式
    QString tabBtnStyle =
            "QPushButton {"
            "  background-color: #f0f0f0;"
            "  border: none;"
            "  padding: 8px 12px;"
            "  border-radius: 10px;"
            "  font-weight: bold;"
            "}"
            "QPushButton:hover {"
            "  background-color: #d6e4f0;"
            "}";
    accountLoginBtn->setStyleSheet(tabBtnStyle);
    emailLoginBtn->setStyleSheet(tabBtnStyle);
    registerBtn->setStyleSheet(tabBtnStyle);

    // 添加切换按钮到布局中
    tabLayout->addWidget(accountLoginBtn);
    tabLayout->addWidget(emailLoginBtn);
    tabLayout->addWidget(registerBtn);
    mainLayout->addLayout(tabLayout);

    // 创建 stacked layout，用于切换三种登录方式
    QStackedLayout* stackedLayout = new QStackedLayout();

    // ================== 账户密码登录界面 ==================
    QWidget* accountWidget = new QWidget(); // 创建账户密码登录界面
    QVBoxLayout* accountLayout = new QVBoxLayout(accountWidget);// 创建账户密码登录界面布局,QVBoxLayout是垂直布局
    QLineEdit* usernameEdit = new QLineEdit();
    usernameEdit->setPlaceholderText("用户名");
    QLineEdit* passwordEdit = new QLineEdit();
    passwordEdit->setPlaceholderText("密码");
    passwordEdit->setEchoMode(QLineEdit::Password);
    QPushButton* loginBtn1 = new QPushButton("登录");

    // 美化输入框样式
    QString inputStyle =
            "QLineEdit {"
            "  padding: 6px 10px;"
            "  border: 1px solid #ccc;"
            "  border-radius: 6px;"
            "  font-size: 14px;"
            "}";

    usernameEdit->setStyleSheet(inputStyle);
    passwordEdit->setStyleSheet(inputStyle);
    loginBtn1->setStyleSheet("QPushButton { background-color: #4CAF50; color: white; border-radius: 6px; padding: 8px; }"
                             "QPushButton:hover { background-color: #45a049; }");

    accountLayout->addWidget(usernameEdit);
    accountLayout->addWidget(passwordEdit);
    accountLayout->addWidget(loginBtn1);

    // ================== 邮箱验证码登录界面 ==================
    QWidget* emailWidget = new QWidget();
    QVBoxLayout* emailLayout = new QVBoxLayout(emailWidget);
    QLineEdit* emailEdit = new QLineEdit();
    emailEdit->setPlaceholderText("邮箱地址");
    QLineEdit* codeEdit = new QLineEdit();
    codeEdit->setPlaceholderText("验证码");
    QPushButton* getCodeBtn = new QPushButton("获取验证码");
    QPushButton* loginBtn2 = new QPushButton("登录");

    emailEdit->setStyleSheet(inputStyle);
    codeEdit->setStyleSheet(inputStyle);
    getCodeBtn->setStyleSheet("QPushButton { background-color: #2196F3; color: white; border-radius: 6px; padding: 6px; }"
                              "QPushButton:hover { background-color: #1976D2; }");
    loginBtn2->setStyleSheet("QPushButton { background-color: #4CAF50; color: white; border-radius: 6px; padding: 8px; }"
                             "QPushButton:hover { background-color: #45a049; }");

    emailLayout->addWidget(emailEdit);
    emailLayout->addWidget(codeEdit);
    emailLayout->addWidget(getCodeBtn);
    emailLayout->addWidget(loginBtn2);

    // ================== 用户注册界面 ==================
    QWidget* registerWidget = new QWidget();
    QVBoxLayout* registerLayout = new QVBoxLayout(registerWidget);
    QLineEdit* newUserEdit = new QLineEdit();
    newUserEdit->setPlaceholderText("新用户名");
    QLineEdit* newEmailEdit = new QLineEdit();
    newEmailEdit->setPlaceholderText("邮箱");
    QLineEdit* newPasswordEdit = new QLineEdit();
    newPasswordEdit->setPlaceholderText("设置密码");
    newPasswordEdit->setEchoMode(QLineEdit::Password);
    QPushButton* registerSubmitBtn = new QPushButton("注册");

    newUserEdit->setStyleSheet(inputStyle);
    newEmailEdit->setStyleSheet(inputStyle);
    newPasswordEdit->setStyleSheet(inputStyle);
    registerSubmitBtn->setStyleSheet("QPushButton { background-color: #FF9800; color: white; border-radius: 6px; padding: 8px; }"
                                     "QPushButton:hover { background-color: #F57C00; }");

    registerLayout->addWidget(newUserEdit);
    registerLayout->addWidget(newEmailEdit);
    registerLayout->addWidget(newPasswordEdit);
    registerLayout->addWidget(registerSubmitBtn);

    // 添加所有页面到 stacked layout 中
    stackedLayout->addWidget(accountWidget);   // 索引0：账户密码登录
    stackedLayout->addWidget(emailWidget);     // 索引1：邮箱登录
    stackedLayout->addWidget(registerWidget);  // 索引2：注册页面

    mainLayout->addLayout(stackedLayout);  // 添加堆叠布局到主布局中




    // 如下开始实现按钮的操作功能
    //
    // ========== 按钮点击切换界面 ==========
    connect(accountLoginBtn, &QPushButton::clicked, [=]() {
        stackedLayout->setCurrentIndex(0);  // 切换到账户登录
    });
    connect(emailLoginBtn, &QPushButton::clicked, [=]() {
        stackedLayout->setCurrentIndex(1);  // 切换到邮箱登录
    });
    connect(registerBtn, &QPushButton::clicked, [=]() {
        stackedLayout->setCurrentIndex(2);  // 切换到注册页面
    });

    // ========== 功能演示：登录、注册 ==========
    connect(loginBtn1, &QPushButton::clicked, [=]() {
        // 执行首界面的登录按钮相关的功能
        // 跳转进入登录后的界面
        //
        QString username = usernameEdit->text();
        QString password = passwordEdit->text();

        // 在终端输出登录信息
        qDebug() << "用户名: " << username << " 密码: " << password;


        this->hide();// 隐藏当前窗口
        //ch13_MainView* mainView = new ch13_MainView();
        //mainView->show();// t创建并显示主界面

        //QString u = usernameEdit->text();
        //QString p = passwordEdit->text();
        //QMessageBox::information(this, u8"登录信息", u8"用户名: " + u + u8"\n密码: " + p);


    });

    connect(getCodeBtn, &QPushButton::clicked, [=]() {
        QMessageBox::information(this, "验证码", "验证码已发送到邮箱！");
    });

    connect(loginBtn2, &QPushButton::clicked, [=]() {
        QString e = emailEdit->text();
        QString c = codeEdit->text();
        QMessageBox::information(this, "邮箱登录", "邮箱: " + e + "\n验证码: " + c);
    });

    connect(registerSubmitBtn, &QPushButton::clicked, [=]() {
        QString user = newUserEdit->text();
        QString em = newEmailEdit->text();
        QString pw = newPasswordEdit->text();
        QMessageBox::information(this, "注册成功", "用户名: " + user + "\n邮箱: " + em);
    });
}


LoginFormandRegister::~LoginFormandRegister()
{
    /*
    * 此处的登录界面可以进一步进行完善,这么做的主要目的在于方便日后可以直接的进行代码的复用
    */

}

